package chapter1.fundamentals.exercise;

/**
 * @author quanlinlin on 2018/11/2 21:00.
 * @version 1.0
 */
public class E_27 {

    private static int        anInt = 0;
    private static Double[][] arr;

    public static void main(String[] args) {
        int N = 100;
        int k = 50;
        arr = new Double[N + 1][k + 1];

        System.out.println(binomial(N, k, 0.25));
        System.out.println(anInt);
    }

    public static double binomial(int N, int k, double p) {
        System.out.println(anInt++);
        if (N == 0 && k == 0) {
            return 1.0;
        }
        if (N < 0 || k < 0) {
            return 0.0;
        }
        if (arr[N][k] == null) {
            arr[N][k] = (1.0 - p) * binomial(N - 1, k, p) + p * binomial(N - 1, k - 1, p);
        }

        return arr[N][k];
    }

}
